package com.zxn.kotlin

import java.lang.Character.UnicodeBlock
import java.util.regex.Matcher
import java.util.regex.Pattern

/**
 * 将字符串拆分为单词数组.
 * @param content 待拆分的字符串
 * @return 返回单词数组
 */
fun splitSentence(content: String) {
    content.split(" ")
}

/**
 * 使用正则表达式来判断字符串中是否包含字母
 * @param str 待检验的字符串
 * @return 返回是否包含
 * true: 包含字母 ;false 不包含字母
 */
fun containsLetters(str: String): Boolean {
    val regex = ".*[a-zA-Z]+.*"
    val m: Matcher = Pattern.compile(regex).matcher(str)
    return m.matches()
}

fun tips(tipsList: List<String>): String {
    if (tipsList.isEmpty()) return "null"
    //随机获取
    tipsList.shuffled().take(1)[0].also {
        val mStringBuilder = StringBuilder()
        if (containsLetters(it)) {
            val wordList = it.split(" ")
            return when (wordList.size) {
                in 0..3 -> it
                in 4..6 -> {
                    wordList.forEachIndexed { index,word ->
                        if (index == 2){
                            mStringBuilder.append("$word\n")
                        }else{
                            mStringBuilder.append("$word ")
                        }
                    }
                    mStringBuilder.toString()
                }
                else -> {
                    wordList.forEachIndexed{ index,word ->
                        if (index <= 5){
                            if (index == 2){
                                mStringBuilder.append("$word\n")
                            }else{
                                mStringBuilder.append("$word ")
                            }
                        }else{
                            mStringBuilder.append("...")
                        }
                    }
                    mStringBuilder.toString()
                }
            }
        } else {
            return when (it.length) {
                in 0..7 -> it
                in 8..14 -> mStringBuilder.append(it.substring(0, 7))
                        .append("\n")
                        .append(it.substring(7)).toString()
                else -> mStringBuilder.append(it.substring(0, 7))
                        .append("\n")
                        .append(it.substring(7, 13))
                        .append("...")
                        .toString()
            }
        }
    }
}

fun isChinese(c: Char): Boolean {
    val ub = UnicodeBlock.of(c)
    return (ub === UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub === UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS || ub === UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub === UnicodeBlock.GENERAL_PUNCTUATION // GENERAL_PUNCTUATION 判断中文的“号
            || ub === UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION // CJK_SYMBOLS_AND_PUNCTUATION 判断中文的。号
            || ub === UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS)
}

fun isChinese(str: String): Boolean {
    val ch = str.toCharArray()
    for (c in ch) {
        if (isChinese(c)) return true
    }
    return false
}



fun main() {
    
}

//fun main() {
//    println(isChinese("炎炎夏日，来杯H2O~"))
//}

//fun main() {
//    val tipsList = mutableListOf(
//            "Hello World",
//            "Nothing great was ever achieved without enthusiasm",
//            "Every horse thinks its own pack heaviest",
//            "Storms make trees take deeper roots",
//            "The tongue is boneless but it breaks bones",
//    )
//    println(tips(tipsList).also {
//        println(it.length)
//    })
//}


//fun main() {
//    println("Hello World".split(" ").size)
//    println("Nothing great was ever achieved without enthusiasm".split(" ").size)
//}

//fun main() {
//    println(containsLetters("Hello"))
//    println(containsLetters("张飞"))
//    println(containsLetters("张飞 is a man!"))
//}

//fun main() {
//    val list = mutableListOf<String>()
//    val list = mutableListOf<String>("1", "2", "3", "4", "5")
//    val tipsList = mutableListOf<String>("5")
//    val tipsList = mutableListOf(
//            //"咕嘟",
//            //"一二三四五六七",
//            //"一二三四五六七八",
//            //"一二三四五六七八九十",
//            //"一二三四五六七八九十一二三四",
//            "一二三四五六七八九十一二三四五六",
//            //"快乐喝水，快乐生活啦啦啦啦啦"
//            )
//    println(tips(tipsList).also {
//        println(it?.length)
//    })
//}

//fun main(args: Array<String>) {

//    val friendUserIds = with(StringBuilder()) {
//        //friendUserIds: String
//        //好友id   逗号分隔 格式 0,1,2,3,4,5
//        //val array = intArrayOf(0, 1, 2, 3)
//        val array = intArrayOf()
//        for ((index, value) in array.withIndex()) {
//            if (index == array.size - 1)
//                append(value)
//            else
//                append("${value},")
//        }
//        toString().also {
//            println("toString:$it")
//        }
//    }
//    println(friendUserIds)
//
//    friendUserIds.isEmpty().let {
//        println("0:$friendUserIds")
//        println("1:$friendUserIds")
//        println("2:$friendUserIds")
//    }
//
//    if (friendUserIds.isNotEmpty()){
//        println("3:$friendUserIds")
//    }


//    val friendUserIds = StringBuilder().apply {
//        val array = intArrayOf(0, 1, 2, 3)
//        for ((index, value) in array.withIndex()) {
//            //append(if (index == array.size - 1) value else append("${value},"))
//            //append("$value")
//            if (index == array.size - 1){
//                append("$value")
//            }else{
//                append("${value},")
//            }
//        }
//    }
//    println(friendUserIds)


//"Hello Kotlin!"
//"北京市·北京市·东城区"
//    val result = "河北省·北京市·东城区".substringBefore("·")
//    println(result)
//
//    val result1 = "河北省·北京市·东城区".substringBeforeLast("·").substringBeforeLast("·")
//    println(result1)
//
//    val result2 = "河北省·北京市·东城区".substringBeforeLast("·").substringAfterLast("·")
//    println(result2)

//}